package com.javabasic.algorithm.leetcode;

/**
 * @author mir.xiong
 * @version 1.0
 * @description
 * @see
 * @since Created by work on 2024/3/29 23:32
 */
public class MinimumSumOfMountainTripletsI {
    public int minimumSum(int[] nums) {

        int len = nums.length;
        int[] left = new int[len];
        int[] right = new int[len];

        left[0] = Integer.MAX_VALUE;
        right[len-1] = Integer.MAX_VALUE;
        for (int i = 1; i < len; i++) {
            int min = Math.min(left[i - 1], nums[i - 1]);
            if (min >= nums[i]) {
                left[i] = Integer.MAX_VALUE;
            } else {
                left[i] = min;
            }
        }

        for (int i = len - 2; i >= 0; i--) {
            int min = Math.min(right[i + 1], nums[i + 1]);
            if (min >= nums[i]) {
                right[i] = Integer.MAX_VALUE;
            } else {
                right[i] = min;
            }
        }

        int result = Integer.MAX_VALUE;
        for (int i = 0; i < len; i++) {
            if (left[i] >= Integer.MAX_VALUE || right[i] >= Integer.MAX_VALUE) {
                continue;
            }
            result = Math.min(result, left[i] + nums[i] + right[i]);
        }

        return result == Integer.MAX_VALUE ? -1 : result;
    }
}
